<?php
namespace Mentees\Controller;

class MentorsController extends MainController {

    public function index(){
 
        $cate = I('get.cate');
    	$map['show']=1;
    	$train = M('TrainCate')->field('id,name')->where($map)->order('sort asc,id asc')->select();
    	$this->assign('train',$train);
    	$this->assign('cate',$cate);

    	unset($map);
    	if ($cate) {
    		$map['trainItem'] = array('like','%-'.$cate.'-%');
    	}
        $map['status']=1;
		$obj = M('Mentors');
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 9);
        $page->setConfig('prev',L('_prev_page'));
        $page->setConfig('next',L('_next_page'));
        $page->setConfig('first',L('_first_page'));
        $page->setConfig('end',L('_end_page'));
        $page->setConfig('theme','%first% %upPage% %linkPage% %downPage% %end%');
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();	
		foreach ($list as $key => $value) {
            $value['trainItem'] = trainStrtoArr($value['trainItem']);
            $where['id'] = array('in',$value['trainItem']);
            $list[$key]['trainItem'] = M('TrainCate')->field('name')->where($where)->select();
		}	
		$this->assign('list', $list);
		$this->assign('page', $show);
		
    	$this->display();
    }

    //全部导师
    public function lists(){
        $obj = M('Train');
        $map['menteesID']=$this->userID;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 12);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        
        foreach ($list as $key => $value) {
            $user = M('Mentors')->where('userid='.$value['mentorsID'])->find();
            $list[$key]['user']=$user;
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        
        $this->display();
    }

    //学习中导师
    public function learning(){
        $obj = M('Train');
        $map['menteesID']=$this->userID;
        $map['status']=0;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 12);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        
        foreach ($list as $key => $value) {
            $user = M('Mentors')->where('userid='.$value['mentorsID'])->find();
            $list[$key]['user']=$user;
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    //已结业的导师
    public function graduation(){
        $obj = M('Train');
        $map['menteesID']=$this->userID;
        $map['status']=1;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 12);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        
        foreach ($list as $key => $value) {
            $user = M('Mentors')->where('userid='.$value['mentorsID'])->find();
            $list[$key]['user']=$user;
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    public function detail(){
        $id = I('get.id');
        if(empty($id)){
            $this->error("Illegal access");
        }else{
            $map['userid']=$id;
            $list = M('Mentors')->where($map)->find();
            if (!$list) {
                $this->error(L('_not_exist'));
            }
            if(time()-$list['last_action_time']>60*20){
                $list['isOnline']=0;
            }else{
                $list['isOnline']=1;
            }
            $list['star']=floor(M("Comment")->where("toID=".$id."")->avg("star"));
            $list['trainItem'] = trainStrtoArr($list['trainItem']);
            $where['id'] = array('in',$list['trainItem']);
            $list['trainItem'] = M('TrainCate')->field('name')->where($where)->select();
            $this->assign('list',$list);
            $this->display();
        }
    }
    //拜师请求发送
    public function apply(){
        $toID = I('post.toID');
        $mentors = M('Mentors')->where('userid='.$toID)->find();
        if (!$mentors) {
            echo $this->echo_json_str("ERROR",L('_not_exist'));die;
        }
        $list = M('Mentees')->where('userid='.$this->userID)->find();
        $data['fromID']=$this->userID;
        $data['fromName'] = $list['nickname'];
        $data['fromFace'] = $list['face'];
        $data['toID']=$toID;
        $data['toName']=$mentors['nickname'];
        $data['toFace']=$mentors['face'];
        $data['trainItem']=I('post.trainItem');
        $data['skills']=I('post.skills');
        $data['target']=I('post.target');
        $data['train']=I('post.train');
        $data['work']=I('post.work');
        $data['createTime'] = time(); 
        $data['updateTime'] = time(); 
        $list = M('Apprentice')->add($data);
        if ($list) {
            echo $this->echo_json_str("SUCCESS",L('_action_success'));die;
        }else{
            echo $this->echo_json_str(L('_action_fail'));die;
        }
    }
    public function comment(){
        $toID = I('post.toID');
        $mentors = M('Mentors')->where('userid='.$toID)->find();
        if (!$mentors) {
            echo $this->echo_json_str("ERROR",L('_not_exist'));die;
        }
        $list = M('Mentees')->where('userid='.$this->userID)->find();
        $data['fromID']=$this->userID;
        $data['fromName'] = $list['nickname'];
        $data['fromFace'] = $list['face'];
        $data['toID']=$toID;
        $data['toName']=$mentors['nickname'];
        $data['toFace']=$mentors['face'];
        $data['star']=I('post.star');
        $data['content']=I('post.content');
        $data['createTime'] = time(); 
        $list = M('Comment')->add($data);
        if ($list) {
            $star=floor(M("Comment")->where("toID=".$toID."")->avg("star"));
            M('Mentors')->where('userid='.$toID)->setField("star",$star);
            echo $this->echo_json_str("SUCCESS",L('_action_success'));die;
        }else{
            echo $this->echo_json_str(L('_action_fail'));die;
        }
    }

    //获取评论列表
    public function getComment(){
        $obj = M('Comment');
        $map['toID']=I("id");
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
           $list[$key]['createTime']=mdate($value['createTime']);
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    //获取学员列表
    public function getStudent(){
        $map['mentorsID']=I("id");
        $menteesID=M('Train')->where($map)->group("menteesID")->getField("menteesID",true);
        if($menteesID){
            unset($map);
            $map['userid']=array('in',$menteesID);
            $obj=M("Mentees");

            $count = $obj->where($map)->count();
            import("Common.ORG.Page");
            $page = new \Page($count, 10);
            $show = $page->show();
            $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
            foreach ($list as $key => $value) {
               $list[$key]['lastTime']=mdate($value['last_action_time']);
            }
            $this->assign('list', $list);
            $this->assign('page', $show);
        }
        $this->display();
    }

    //获取推荐导师
    public function getTuijian(){
        $cate=I("cate");
        $map['trainItem'] = array('like','%-'.$cate.'-%');
        $map['status']=1;
        $list=M("Mentors")->where($map)->order("star desc")->limit(3)->select();
        $this->assign('list', $list);
        $this->display();
    }
}
